/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.util.analyze;

import java.text.NumberFormat;
import java.util.Iterator;

import soccerscope.model.ClientScene;
import soccerscope.model.GameEvent;
import soccerscope.model.WorldModel;

public class PassVelAnalyzer extends PassAnalyzer {

	public static String NAME = "Pass Vel";

	private float leftAve;

	private float leftMin;

	private float leftMax;

	private float rightAve;

	private float rightMin;

	private float rightMax;

	@Override
	public void init() {
		super.init();
	}

	@Override
	public String getName() {
		return NAME;
	}

	@Override
	public Object getValueAt(int col, int fromTime, int toTime) {
		count(fromTime, toTime);
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumIntegerDigits(2);
		nf.setMaximumFractionDigits(2);
		switch (col) {
		case ROW_NAME:
			return getName();
		case LEFT:
			if (this.lcount == -1)
				return "--";
			// return "(" + nf.format(leftMin) + " : " + nf.format(leftAve /
			// lcount) +
			// " : " + nf.format(leftMax) + ")";
			return nf.format(this.leftAve / this.lcount);
		case RIGHT:
			if (this.rcount == -1)
				return "--";
			// return "(" + nf.format(rightMin) + " : " + nf.format(rightAve /
			// rcount) +
			// " : " + nf.format(rightMax) + ")";
			return nf.format(this.rightAve / this.rcount);
		default:
			return " ";
		}
	}

	@Override
	public void count(int fromTime, int toTime) {
		Iterator it = PassAnalyzer.getPassLeftList().iterator();
		this.lcount = 0;
		this.leftAve = 0;
		this.leftMin = 100;
		this.leftMax = -100;
		while (it.hasNext()) {
			Pass pass = (Pass) it.next();
			if (fromTime <= pass.sender.time && pass.receiver.time <= toTime) {
				ClientScene clientScene = (ClientScene) WorldModel
						.getInstance().getSceneSet().getScene(pass.sender.time);
				float vel = clientScene.ball.vel.r();
				this.leftAve += vel;
				this.lcount++;
				if (this.leftMin > vel)
					this.leftMin = vel;
				if (this.leftMax < vel)
					this.leftMax = vel;
			}
		}
		if (this.lcount == 0)
			this.lcount = -1;

		it = PassAnalyzer.getPassRightList().iterator();
		this.rcount = 0;
		this.rightAve = 0;
		this.rightMin = 100;
		this.rightMax = -100;
		while (it.hasNext()) {
			Pass pass = (Pass) it.next();
			if (fromTime <= pass.sender.time && pass.receiver.time <= toTime) {
				ClientScene clientScene = (ClientScene) WorldModel
						.getInstance().getSceneSet().getScene(pass.sender.time);
				float vel = clientScene.ball.vel.r();
				this.rightAve += vel;
				this.rcount++;
				if (this.rightMin > vel)
					this.rightMin = vel;
				if (this.rightMax < vel)
					this.rightMax = vel;
			}
		}
		if (this.rcount == 0)
			this.rcount = -1;
	}

	@Override
	public GameEvent analyze(ClientScene clientScene, ClientScene prev) {
		return null;
	}
}
